package core.proto.annotation;

import static app.constants.CollabVizConstants.ANNOTATION_MARGIN;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.apache.commons.io.FilenameUtils;
import org.jdesktop.swingx.mapviewer.GeoPosition;

import core.proto.AllComponents;
import core.proto.PaintDoc;
import core.proto.annotation.AnnotationModel.AnnotationLocation;

import static app.constants.CollabVizConstants.*;

/**
 * 
 * 
 *
 */
public class DocAnnotationUI extends AnnotationUI {

	public DocAnnotationUI(AnnotationModel model) throws CannotAddAnnotationException {
		super(model);
		
		int x = 0;
		int y = 0;
		if (model instanceof DocAnnotationModel){
			DocAnnotationModel m = (DocAnnotationModel) model;
			x = m.getX();
			y = m.getY();
		}
		this.setupLabel(x,y);
		this.frame.setLocation(x,y);
	}

	/**
	 * Subclass can call this to setup the label if needed. 
	 */
	protected void setupLabel (int x, int y) {
		label = new JLabel();
		label.setOpaque(true);
		label.setBackground(LABEL_COLOR);
		label.setBounds(x,y,LABEL_W, LABEL_H);
		label.setSize(LABEL_W, LABEL_H);
		label.setBorder(BorderFactory.createLineBorder(Color.black));

		label.addMouseListener(new MouseAdapter() {
			public void mouseClicked (MouseEvent me) {
				if (me.getButton() == MouseEvent.BUTTON1) {
					show(label.getX(), label.getY());
				}
			}
			
			public void mouseEntered(MouseEvent e) {
				if(label.isVisible()){
					highlightCorrespondingText(true);
				}
		    }

		    public void mouseExited(MouseEvent e) {
		    	if(label.isVisible()){
		    		highlightCorrespondingText(false);
		    	}
		    }
		});
	}
	
	public void highlightCorrespondingText(boolean toHilite){
		for(Annotation anno : AnnotationManager.getAnnotations(FilenameUtils.getBaseName(PaintDoc.staticDocKey), AnnotationLocation.DOC)){
			DocAnnotationModel m = (DocAnnotationModel) anno.getModel();
			DocAnnotationUI j = (DocAnnotationUI) anno.getView();
			
			if(j == this){
				PaintDoc doc = AllComponents.getDocumentPaintDoc();
				
				if(toHilite){
					doc.setHoverONHighlite(m.getStartIndex(), m.getEndIndex());
				}
				else{
					doc.setHoverOFFHighlite(m.getStartIndex(), m.getEndIndex());
				}
				
			}
		}
	}
	
	@Override
	public void addToPane (JLayeredPane pane) {
		super.addToPane(pane);
		pane.add(label, JLayeredPane.PALETTE_LAYER);
	}

	protected void doFocusLostModelViewUpdates () {
		/**SET CORRESPONDING TEXT TO NORMAL COLOR **/
		highlightCorrespondingText(false);
		DocAnnotationModel model = (DocAnnotationModel) this.model; 
		model.setX(frame.getX());
		model.setY(frame.getY());

		shrink(model.getX(), model.getY());
	}
	
	public void hide() {
		frame.setVisible(false);
		label.setVisible(false);
	}

	@Override
	public void shrink(double x, double y) {
		label.setLocation((int) x, (int) y);
		label.setBounds((int) x, (int) y, LABEL_W, LABEL_H);
		String txt = textArea.getText();
		model.setUserText(txt);
		label.setText(abbreviateLabelText(txt));

		parent.moveToFront(label);
		
		frame.setVisible(false);
		label.setVisible(true);
	}

	public void removeFromPane() {
		parent.remove(frame);
		parent.remove(label);
		parent.repaint();
	}

	public void show(int x, int y) {
		label.setVisible(false);
		super.show(x, y);
	}
	
	public void changeToAltBackground(){
		label.setBackground(LABEL_COLOR_ALT);
	}
	
	public void revertBackground(){
		label.setBackground(LABEL_COLOR);
	}
}
